<template>
  <div class="login-container">
    <div class="login-form">
      <div class="title-container">
        <h3 class="title">Electron Vue Admin</h3>
      </div>
      
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on" label-position="left">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            type="text"
            auto-complete="on"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            :type="passwordVisible ? 'text' : 'password'"
            auto-complete="on"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
            <template #suffix>
              <el-icon @click="passwordVisible = !passwordVisible">
                <component :is="passwordVisible ? 'View' : 'Hide'" />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <!-- 记住密码 -->
        <div class="remember-container">
          <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
        </div>
        
        <!-- 登录按钮 -->
        <el-button :loading="loading" type="primary" class="login-button" @click="handleLogin">
          {{ loading ? '登录中...' : '登录' }}
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'Login',
  setup() {
    const store = useStore()
    const router = useRouter()
    
    // 登录表单
    const loginForm = reactive({
      username: 'admin',
      password: '123456',
      remember: false
    })
    
    // 表单验证规则
    const loginRules = {
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
    }
    
    // 密码可见性
    const passwordVisible = ref(false)
    
    // 加载状态
    const loading = ref(false)
    
    // 登录表单引用
    const loginFormRef = ref(null)
    
    // 处理登录
    const handleLogin = () => {
      loginFormRef.value?.validate(valid => {
        if (valid) {
          loading.value = true
          store.dispatch('login', loginForm)
            .then(() => {
              ElMessage.success('登录成功')
              router.push('/')
            })
            .catch(error => {
              console.error('登录失败:', error)
              ElMessage.error('登录失败，请检查用户名和密码')
            })
            .finally(() => {
              loading.value = false
            })
        } else {
          return false
        }
      })
    }
    
    return {
      loginForm,
      loginRules,
      passwordVisible,
      loading,
      loginFormRef,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2d3a4b;
}

.login-form {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.title-container {
  margin-bottom: 30px;
  text-align: center;
}

.title {
  margin: 0;
  color: #303133;
  font-size: 24px;
  font-weight: bold;
}

.remember-container {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  margin-bottom: 10px;
}
</style>